package com.ljc.cjgl.controller;

import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.ActivityAttachment;
import com.ljc.cjgl.entitiy.ClassActitvity;
import com.ljc.cjgl.entitiy.Stu;
import com.ljc.cjgl.entitiy.TbClass;
import com.ljc.cjgl.form.ClassActivityForm;
import com.ljc.cjgl.security.SysUser;
import com.ljc.cjgl.security.UserUtils;
import com.ljc.cjgl.service.ActivityAttachmentService;
import com.ljc.cjgl.service.ClassActivityService;
import com.ljc.cjgl.service.ClassService;
import com.ljc.cjgl.service.StuService;

@Controller
@RequestMapping(value="/activity")
public class ClassActivityController {
	@Autowired
	private ClassActivityService service;
	@Autowired
	private ClassService classService;
	@Autowired
	private StuService stuService;
	@Autowired
	private ActivityAttachmentService attachmentService;
	@Autowired
	private UserUtils userUtils;
	public Specification<ClassActitvity> buildSpec(ClassActivityForm form) {
		HashSet<Rule> rules=new HashSet<>();
		Integer classId = form.getClassId();
		String name = form.getName();
		Integer type=form.getType();
		String search=form.getSearch();
		if(classId!=null) {
			rules.add(Rule.property("tbClass.id").equal(classId));
		}
		if(type!=null) {
			rules.add(Rule.property("type").equal(type));
		}
		if(StringUtils.hasText(name)) {
			rules.add(Rule.property("name").contain(name));
		}
		
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("name").contain(search));
			rules2.add(Rule.property("tbClass.name").contain(search));
			rules2.add(Rule.property("manager").contain(search));
			rules.add(Rule.or(rules2));
		}
		return Rule.and(rules).toSpecification();
	}
	
	@RequestMapping(value="/manage")
	public String manage(ModelMap map) {
		List<TbClass> classes=classService.findAll();
		Integer classId=null;
		if(userUtils.hasRole("ROLE_STUDENT")) {
			classes.clear();
			Stu student=(Stu)userUtils.getUser();
			classes.add(student.getTbClass());
			classId=student.getId();
		}
		map.put("classes",classes);
		map.put("classId", classId);
		return "activity/manage";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(TablePageable pageable,ClassActivityForm form) {
		SysUser user=userUtils.getUser();
		if(userUtils.hasRole("ROLE_STUDENT")) {
			Stu student=stuService.findById(user.getId());
			form.setClassId(student.getTbClass().getId());
		}
		PageRequest pageRequest = pageable.bulidPageRequest();
		Specification<ClassActitvity> specification = buildSpec(form);
		Page<ClassActitvity> page = service.findAll(specification, pageRequest);
		return TableResult.bulidPageResult(page);
	}
	
	@RequestMapping(value="/edit")
	public String edit(Integer classId,Integer id,ModelMap map) {
		SysUser user=userUtils.getUser();
		if(userUtils.hasRole("ROLE_STUDENT")) {
			classId=((Stu)user).getTbClass().getId();
		}
		ClassActitvity model=new ClassActitvity();
		if(id!=null) {
			model=service.findById(id);
		}
		List<TbClass> classes=classService.findAll();
		if(classId!=null) {
			classes.clear();
			classes.add(classService.findById(classId));
		}
		map.put("model", model);
		map.put("classes", classes);
		return "activity/edit";
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(ClassActivityForm form) {
		SysUser user=userUtils.getUser();
		form.setModifier(user.getName());
		if(form.getId()==null) {
			form.setCreator(user.getName());
		}
		service.save(form);
		return TableResult.bulidResult(true, "数据保存成功");
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(@RequestBody List<ClassActitvity> actitvities) {
		boolean success=service.delete(actitvities);
		if(success) {
			return TableResult.bulidResult(true, "数据删除成功");
		}else {
			return TableResult.bulidResult(false, "数据删除失败");
		}
	}
	
	@RequestMapping(value="/details")
	public String details(Integer id,ModelMap map) {
		ClassActitvity model = service.findById(id);
		model.setDemo(TableResult.txtToHtml(model.getDemo()));
		map.put("model",model);
		List<ActivityAttachment> photoes=attachmentService.findByActivityIdAndType(model.getId(),0);
		map.put("photoes", photoes);
		return "activity/details";
	}

}
